package socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端程序
 */
public class Client {
    private Socket socket;
    public Client() {
        try {
            System.out.println("正在连接服务端>>>");
            socket = new Socket("localhost",8091);
            System.out.println("服务端连接成功!!!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void start(){
        try {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入文字:");
                String str = scanner.nextLine();
                if ("exit".equals(str)){
                    System.out.println("程序退出");
                    break;
                }
                pw.println(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
